using System;
using System.Collections.Generic;
using System.Text;
using Bussiness.Interfaces;
using Bussiness.Data;
using DataProvider;
using System.Data;
using System.Data.SqlClient;
using Bussiness.Exc;

namespace Bussiness
{
    public class DMKHOIProcess:IDMKHOI
    {
        IDBManager idbManager = new DBManager(DataProviderE.SqlServer);

        DataTable IDMKHOI.XemDMKHOI()
        {
            DataTable tbl = null;
            try
            {
                idbManager.Open();
                tbl = idbManager.ExecuteDataSet(CommandType.StoredProcedure, "spDMKHOIXem").Tables[0];
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. IDMKHOI.XemDMKHOI()" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return tbl;
        }
        bool IDMKHOI.ThemDMKHOI(DMKHOI dmkhoi)
        {
            bool success = false;
            try
            {
                idbManager.CreateParameters(2);
                idbManager.AddParameters(0, "@Makhoi", dmkhoi.Makhoi, ParameterDirection.Input);
                idbManager.AddParameters(1, "@Tenkhoi", dmkhoi.Tenkhoi, ParameterDirection.Input);
                idbManager.Open();
                int isuccess = idbManager.ExecuteNonQuery(CommandType.StoredProcedure, "spDMKHOIThem");
                if (isuccess > 0)
                    success = true;
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. IDMKHOI.ThemDMKHOI(DMKHOI dmkhoi)" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return success;
        }
        bool IDMKHOI.XoaDMKHOI(string makhoiValue)
        {
            bool success = false;
            try
            {
                idbManager.CreateParameters(1);
                idbManager.AddParameters(0, "@Makhoi", makhoiValue, ParameterDirection.Input);
                idbManager.Open();
                int isuccess = idbManager.ExecuteNonQuery(CommandType.StoredProcedure, "spDMKHOIXoa");
                if (isuccess > 0)
                    success = true;
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. IDMMON.XoaDMMON(string makhoinValue)" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return success;
        }
        bool IDMKHOI.UpdateDMKHOI(DMKHOI dmkhoi)
        {
            bool success = false;
            try
            {
                idbManager.CreateParameters(2);
                idbManager.AddParameters(0, "@Makhoi", dmkhoi.Makhoi, ParameterDirection.Input);
                idbManager.AddParameters(1, "@Tenkhoi", dmkhoi.Tenkhoi, ParameterDirection.Input);
                idbManager.Open();
                int isuccess = idbManager.ExecuteNonQuery(CommandType.StoredProcedure, "spDMKHOIUpdate");
                if (isuccess > 0)
                    success = true;
            }
            catch (DatabaseExc e)
            {
                throw new BussinessExc(e.Message, e.ErrCodeUser, e.ErrCodeSQL);
            }
            catch (Exception e)
            {
                throw new BussinessExc("Bussiness. IDMKHOI.UpdateDMKHOI(DMKHOI dmkhoi)" + e.Message);
            }
            finally
            {
                idbManager.Dispose();
            }
            return success;
        }
    }
}
